<?php
	require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/database.php';
	require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/doctype.php';

	//Ensure user is logged in
	if (!isset($_SESSION['user'])) {
		header("Location:http://" . $_SERVER['HTTP_HOST'] . '/deviantpic/index.php');
		die();
	}

	//If file is uploaded
    if (isset($_POST['submit'])) {
		//Move uploaded file
		$path = "files/" . basename($_FILES['uploadFile']['name']);
		move_uploaded_file($_FILES['uploadFile']['tmp_name'], $path);

		//Insert to database
		mysql_query("INSERT INTO photos (photo, title, description, category_id, date_taken) VALUES ('" .
				"/deviantpic/picture/" . $path . "','" .
				$_POST['title'] . "','" .
				$_POST['description'] . "','" .
				$_POST['category'] . "','" .
				$_POST['dateTaken'] . "')");
		$id = mysql_insert_id();

		//Set user as owner
		mysql_query("INSERT INTO tags (photo_id, user_id, is_owner, is_tagged) VALUES ('" . $id . "','" . $_SESSION['user'] . "','1','0')");				

		//While there's still a receiver
		$text	= $_POST['keyword'];
		$token	= strtok($text, ",");
		while ($token) {
			//Trim whitespace then insert it
			$keyword = trim($token);
			mysql_query("INSERT INTO keywords (photo_id, keyword) VALUES ('" . $id . "','" . $keyword . "')");

			//Next
			$token = strtok(",");
		}

		//Redirect
		header("Location:http://" . $_SERVER['HTTP_HOST'] . "/deviantpic/picture/view.php?id=$id");
		die();
	}

	//Get list of categories
	$categories = array();

	$result = mysql_query("SELECT * FROM categories");
	$row	= mysql_fetch_array($result);
	while ($row) {
		$categories[] = $row;
		$row = mysql_fetch_array($result);
	}

	function checkExtension($image) {
		$part = explode(".", $image);
		$extension = $part[1];

		if ($extension == "jpg" || $extension == "png") {
			return true;
		}
	}
?>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>deviantPIC - Upload picture</title>
		<link rel="StyleSheet" href="../css/indah.css" type="text/css" />
        <script type="text/javascript" src="/deviantpic/script/dateselector.js"></script>
        <script type="text/javascript" src="/deviantpic/script/upload_validation.js"></script>
    </head>
    <body>
		<!-- Header -->
		<?php require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/header.php'; ?>

		<!-- Content -->
		<div id="body">
			<h2>Upload Picture</h2>
			<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" onsubmit="return check(this)">
				Picture : <input type="file" name="uploadFile"/><br/>
				Name : <input type="text" name="title"/><br/>
				Description : <input type="text" name="description"/><br/>
				Keywords : <input type="text" name="keyword"/><br/>

				<!-- Categories -->
				<label for="input_category">Categories: </label>
				<select name="category" id="input_category">
					<?php
						foreach ($categories as $category) {
							echo '<option value="' . $category['id'] . '">' . $category['name'] . '</option>';
						}
					?>					
				</select> <br />

				Date taken : <input type="text" name="dateTaken" id="calDate"/><br/>
				<input type="button" value="date selector" onclick="document.getElementById('cal').innerHTML = dispCal(0,0);"/><br/>
				<div id="cal" style="position:relative; left:00px;top:00px;"></div>
				<input type="submit" name="submit" value="Upload File"/><br/>
			</form>
		</div>

		<!-- Footer -->
		<?php require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/footer.php'; ?>
    </body>
</html>